//
//  SelectAddress.m
//  ChampoinPeso
//
//  Created by mac on 2025/2/20.
//

#import "SelectAddress.h"
#import "BRStringPickerView.h"

@interface SelectAddress()
@property (nonatomic,strong)NSArray<BRResultModel *> *resultModelArr;
@property (nonatomic,strong)BRStringPickerView *stringPickerView;
@end
@implementation SelectAddress

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = UIColor.clearColor;
        [self setupUI];
    }
    return self;
}
-(void)setupUI
{
    UIView *view = [UIView new];
    [self addSubview:view];
    view.backgroundColor = UIColor.clearColor;
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.mas_offset(0);
    }];
    UIImageView *bgIV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 454)];
    bgIV.image = [UIImage imageNamed:@"selectAddress"];
    [self addSubview:bgIV];
    [self sendSubviewToBack:bgIV];
    UIButton *close = [UIButton buttonWithType:UIButtonTypeCustom];
    [close setImage:[UIImage imageNamed:@"selectClose"] forState:UIControlStateNormal];
    [self addSubview:close];
    [close mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_offset(16);
        make.right.mas_offset(-16);
    }];
    [view addSubview:self.pView];
    [view bringSubviewToFront:self.pView];
//    [_pView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_offset(71);
//        make.left.right.mas_offset(0);
//        make.bottom.mas_offset(-bottomSafeHeight - 52);
//    }];
    UIButton *selectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectBtn setImage:[UIImage imageNamed:@"selectTurn"] forState:UIControlStateNormal];
    [self addSubview:selectBtn];
    [selectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_offset(-bottomSafeHeight);
        make.left.mas_offset(23);
        make.right.mas_offset(-23);
        make.height.mas_equalTo(52);
    }];
    [close addTarget:self action:@selector(close) forControlEvents:UIControlEventTouchUpInside];
    [selectBtn addTarget:self action:@selector(commit) forControlEvents:UIControlEventTouchUpInside];

}
-(BRStringPickerView *)stringPickerView
{
    if (!_stringPickerView) {
        _stringPickerView = [[BRStringPickerView alloc]init];
    }
    return _stringPickerView;
}
-(void)close
{
    [_stringPickerView dismiss];
    if (self.closeAction) {
        self.closeAction();
    }
}
-(void)commit
{
    [_stringPickerView dismiss];
    if (self.dddressCommitAction) {
        self.dddressCommitAction(_resultModelArr);
    }
}


-(UIView *)pView
{
    if (!_pView) {
        _pView = [[UIView alloc]initWithFrame:CGRectMake(0, 71, SCREEN_WIDTH, 250)];
        _pView.backgroundColor = UIColor.clearColor;
    }
    return _pView;
}

-(void)setSource:(NSDictionary *)source name:(NSString *)name{
    NSMutableArray *teamsres = [NSMutableArray array];
    NSArray *items = source[@"chieftain"];
    NSMutableArray *res = [NSMutableArray array];
    NSMutableArray *indexs = [NSMutableArray array];
    NSArray *strs ;
    if (![name isEqualToString:@""]) {
        strs = [name componentsSeparatedByString:@"-"];
    }
    [items enumerateObjectsUsingBlock:^(NSDictionary*  _Nonnull dic, NSUInteger idx, BOOL * _Nonnull stop) {
        BRResultModel *model = [[BRResultModel alloc]init];
        model.parentKey = @"-1";
        model.parentValue = @"";
        model.key = CPS(dic[@"adjadjadj"]);
        model.value = CPS(dic[@"madnevertheless"]);
        [teamsres addObject:model];
        NSArray *arr = [NSArray arrayWithArray:dic[@"chieftain"]];
        if (idx == 0) {
            [res addObject:model];
        }
        [indexs addObject:@0];
        NSLog(@"i=%lu",(unsigned long)idx);
        [arr enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull dict, NSUInteger idx1, BOOL * _Nonnull stop) {
            BRResultModel *model1 = [[BRResultModel alloc]init];
            model1.parentKey = CPS(dic[@"adjadjadj"]);
            model1.parentValue = CPS(dic[@"madnevertheless"]);
            NSInteger newid = [dict[@"adjadjadj"] integerValue] *100 *(idx+2);
            model1.key = [NSString stringWithFormat:@"%ld",(long)newid];
            model1.value = CPS(dict[@"madnevertheless"]);
            [teamsres addObject:model1];
            NSArray *array = [NSArray arrayWithArray:dict[@"chieftain"]] ;
            if (idx1 == 0) {
                [res addObject:model1];
            }
            [indexs addObject:@0];
            NSLog(@"j=%lu",(unsigned long)idx1);
            [array enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull dicte, NSUInteger idx2, BOOL * _Nonnull stop) {
                BRResultModel *model2 = [[BRResultModel alloc]init];
                model2.parentKey = [NSString stringWithFormat:@"%ld",(long)newid];
                model2.parentValue = CPS(dict[@"madnevertheless"]);
                model2.key = CPS(dicte[@"adjadjadj"]);
                model2.value = CPS(dicte[@"madnevertheless"]);
                [teamsres addObject:model2];
                if (idx2 == 0) {
                    [res addObject:model2];
                }
                [indexs addObject:@0];
                NSLog(@"k=%lu",(unsigned long)idx2);
            }];
        }];
    }];
    _resultModelArr = res;
    self.stringPickerView.backgroundColor = UIColor.clearColor;
    _stringPickerView.pickerMode = BRStringPickerComponentLinkage;
    _stringPickerView.dataSourceArr = teamsres;
    _stringPickerView.isAutoSelect = YES;
    _stringPickerView.numberOfComponents = 3;
    _stringPickerView.changeModelArrayBlock = ^(NSArray<BRResultModel *> *resultModelArr) {
        NSLog(@"%@",resultModelArr);
        self->_resultModelArr = resultModelArr;
    };
    BRPickerStyle *customStyle = [[BRPickerStyle alloc]init];
    customStyle.pickerColor = UIColor.clearColor;
    customStyle.pickerTextColor = UIColor.blackColor;
    customStyle.separatorColor = UIColor.clearColor;
    customStyle.pickerHeight=400;
    _stringPickerView.pickerStyle = customStyle;
    [_stringPickerView addPickerToView:self.pView];
}
@end
